import Mock from 'mockjs'
import { users } from './user'

const List = []
const count = 100

const orgs = ['总经理办公室','行政部','人力资源部','财务部','生产技术部','计划营销部','安全监察部','营运部']
const orgCodes = ['zjlbgs','xzb','rlzyb','cwb','scjsb','jhyxb','aqjcb','yyb']
for (let i = 0; i < orgs.length; i++) {
  List.push(Mock.mock({
    id: '@increment',
    'code': orgCodes[i],
    'name': orgs[i],
    'users:1-2': users,
    'parent': 'root'
  }))
}

for (let i = orgs.length; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    'code': Mock.Random.word(3, 10),
    'name': Mock.Random.ctitle(5, 9) + '部',
    'users:1-2': users,
    'parent': orgCodes[i % orgs.length]
  }))
}

const subTree = (orgTree) => {
  let children = List.filter(it => it.parent === orgTree.code).map(it => {return {code: it.code, title: it.name, children: null}})
  orgTree.children = children.length > 0 ? children : null

  if(orgTree.children && orgTree.children.length > 0) {
    orgTree.children.forEach(s => {
      subTree(s)
    });
  }
}
const rootTree = {
  code: 'root',
  title: '跟组织',
  children: []
}

subTree(rootTree)

export default [
  {
    url: '/auth/api/w/auth/organizations/tree',
    type: 'get',
    response: () => {
      return {
        'status': 'success',
        'data': rootTree
      }
    }
  },
  {
    url: '/auth/api/w/auth/organizations/p',
    type: 'get',
    response: config => {
      const page = Number(config.query.page)
      const size = Number(config.query.size)
      const queries = Object.keys(config.query).filter(it => it.indexOf('s_') === 0).map(it => {return {key: it.replace('s_',''), value: config.query[it]}})
      const filteredData = List.filter(it => {
        return queries.length == 0 || (queries.length > 0 && queries.filter(q => it.hasOwnProperty(q.key) && it[q.key].indexOf(q.value) > -1).length === queries.length)
      })
      const data = filteredData.filter((item, index) => index >= page * size && index < (page+1) * size)
      return {
        'status': 'success',
        'data': data,
        'pageable': {
          'totalElements': filteredData.length,
          'numberOfElements': data.length,
          'totalPages': Math.ceil(filteredData.length / size),
          'number': page,
          'first': page === 0,
          'last': data.length < size || (data.length === size && (page + 1) * size >= filteredData.length),
          'size': size,
          'fromNumber': page * size,
          'toNumber': page * size + data.length
        }
      }
    }
  },
  {
    url: '/auth/api/w/auth/organizations/:id',
    type: 'get',
    response: config => {
      const id = config.params.id
      const item = List.find(it => it.id == id)
      return {
        'status': 'success',
        'data': item
      }
    }
  },
  {
    url: '/auth/api/w/auth/organizations',
    type: 'post',
    response: config => {
      const { code, name, parent } = config.body
      const item = {id: Math.max(...List.map(it=>it.id)) + 1, code, name, parent}
      List.push(item)
      return {
        'status': 'success'
      }
    }
  },
  {
    url: '/auth/api/w/auth/organizations/:id',
    type: 'put',
    response: config => {
      const id = config.params.id
      let item = List.find(it => it.id == id)
      if(item) {
        for(let k in config.body) {
          if(item.hasOwnProperty[k])  item[k] = config.body[k]
        }
      }
      return {
        'status': 'success',
        data: item
      }
    }
  },
  {
    url: '/auth/api/w/auth/organizations/:id',
    type: 'delete',
    response: config => {
      const id = config.params.id
      let idx = List.findIndex(it => it.id == id)
      if(idx > -1) {
        List.shift(idx)
      }
      return {
        'status': 'success'
      }
    }
  }
]
